<?php
/**
 * @package Autogenerator for Joomla!
 * @version 3.0.0
 * @author Truong Cong Duc <truongcongduc4@gmail.com>
 * @link autogenerator.blogspot.com
 * @copyright (c) 2013, Truong Cong Duc
 * @license GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

require_once JPATH_COMPONENT_ADMINISTRATOR . '/helpers/autogenerator.php';

class AutogeneratorModelXml_element extends JModelAdmin {

    protected $text_prefix = 'COM_AUTOGENERATOR';

    public function getForm($aData = array(), $mLoadData = true) {
        $oApplication = JFactory::getApplication();

        $sLayout = $oApplication->input->getString('layout');

        if ($sLayout == 'add' || $sLayout == 'delete') {
            $sXMLForm = 'xml_element_add_delete';
        } else {
            $sXMLForm = 'xml_element_edit';
        }

        $oForm = $this->loadForm('com_autogenerator.xml_element', $sXMLForm, array('control' => 'jform', 'load_data' => $mLoadData));

        if (empty($oForm)) {
            return false;
        }

        return $oForm;
    }

    public function getTable($sType = 'Xml_element', $sPrefix = 'AutogeneratorTable', $aConfig = array()) {
        return JTable::getInstance($sType, $sPrefix, $aConfig);
    }

    protected function loadFormData() {
        $oApplication = JFactory::getApplication();

        $aData = $oApplication->getUserState('com_autogenerator.edit.xml_element.data', array());

        if (empty($aData)) {
            $aData = $this->getItem();
        }

        $aData->xml_type_id = $oApplication->input->getInt('iXmlTypeId');

        return $aData;
    }

    protected function getReorderConditions($oTable) {
        $aCondition = array();

        return $aCondition;
    }

    protected function prepareTable($oTable) {
        
    }

    /**
     * Add new recode.
     * @param type $aData
     * @return type 
     */
    public function add($aData) {
        $oTable = $this->getTable();

        // Allow an exception to be thrown.
        try {
            // Bind the data.
            if (!$oTable->bind($aData)) {
                $this->setError($oTable->getError());
                return false;
            }

            // Prepare the row for saving
            $this->prepareTable($oTable);

            // Check the data.
            if (!$oTable->check()) {
                $this->setError($oTable->getError());
                return false;
            }

            // Store the data.
            if (!$oTable->store()) {
                $this->setError($oTable->getError());
                return false;
            }

            // Clean the cache.
            $this->cleanCache();
        } catch (Exception $e) {
            $this->setError($e->getMessage());

            return false;
        }

        $pkName = $oTable->getKeyName();

        if (isset($oTable->$pkName)) {
            return $oTable->$pkName;
        }

        return true;
    }

    public function editInline($aData) {
        $iKey = (int) $aData['id'];
        if ($iKey < 1) {
            return false;
        }

        $oTable = $this->getTable();

        // Allow an exception to be thrown.
        try {
            // Load the row if saving an existing record.
            if ($iKey > 0) {
                if (!$oTable->load($iKey)) {
                    return false;
                }
            }

            // Bind the data.
            if (!$oTable->bind($aData)) {
                $this->setError($oTable->getError());
                return false;
            }

            // Prepare the row for saving
            $this->prepareTable($oTable);

            // Check the data.
            if (!$oTable->check()) {
                $this->setError($oTable->getError());
                return false;
            }

            // Store the data.
            if (!$oTable->store()) {
                $this->setError($oTable->getError());
                return false;
            }

            // Clean the cache.
            $this->cleanCache();
        } catch (Exception $e) {
            $this->setError($e->getMessage());

            return false;
        }

        $pkName = $oTable->getKeyName();

        if (isset($oTable->$pkName)) {
            return $oTable->$pkName;
        }

        return true;
    }

}
